package com.accesshollywood.grfxOrdering.panels;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.Border;

import org.jdesktop.swingx.VerticalLayout;
import org.joda.time.DateTime;
import org.joda.time.Duration;

import com.accesshollywood.components.FoldablePanel;
import com.accesshollywood.grfxOrdering.GRFXOrdersMain;
import com.accesshollywood.grfxOrdering.OrderMainGUI;
import com.accesshollywood.grfxOrdering.basics.Colors;
import com.accesshollywood.grfxOrdering.basics.GrfxOrderListModel;
import com.accesshollywood.grfxOrdering.basics.STATE;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.indexables.GrfxOrder;
import com.accesshollywood.prefs.Prefs;
import com.accesshollywood.trace.Trace;

public class OrderTree2 extends JPanel {
	private int									topBottomBorder;
	private int									leftRightBorder;
	public JComponent							parent;
	public GrfxOrderListModel					model;
	JScrollPane									scroller;
	public GrfxOrder							selectedOrder;
	LinkedHashMap<Integer, SingleOrderPanel>	displayedOrderPanels	= new LinkedHashMap<Integer, SingleOrderPanel>();

	public OrderTree2(JComponent parent) {
		this.parent = parent;
		setLayout(new VerticalLayout());
	}

	public void setModel(GrfxOrderListModel model) {
		// Trace.info("setModel");
		this.model = model;
		// createPanel();
	}

	public void createPanel() {
		// Trace.stack();
		DateTime t0 = new DateTime();
		topBottomBorder = Prefs.PixelSize.borderTB.get();
		leftRightBorder = Prefs.PixelSize.borderLR.get();

		FoldablePanel.FOLD_STATE foldState;
		if (model != null) {
			displayedOrderPanels = new LinkedHashMap<Integer, SingleOrderPanel>();
			for (int i = 0; i < model.getSize(); i++) {

				GRFXOrdersMain.pBarDlg.setValue(model.getSize() + i);

				GrfxOrder currOrder = (GrfxOrder) model.elementAt(i);

				SingleOrderPanel currOrderPanel = new SingleOrderPanel(currOrder, this);

				Border editableBorder;
				Border spacerBorder;
				Border edgeBorder;

				// if (Users.currentUser.isAdmin || (Users.currentUser.isOrderer && Users.currentUser == currOrder.getOrderedBy())) {

				if (Users.currentUser.canEditOrder(currOrder)) {
					editableBorder = BorderFactory.createMatteBorder(topBottomBorder, leftRightBorder, topBottomBorder, leftRightBorder, Color.GREEN);
				} else {
					editableBorder = BorderFactory.createMatteBorder(topBottomBorder, leftRightBorder, topBottomBorder, leftRightBorder, Color.GRAY);
				}
				spacerBorder = BorderFactory.createMatteBorder(topBottomBorder, 0, topBottomBorder, 0, Color.GRAY);
				edgeBorder = BorderFactory.createMatteBorder(1, 0, 0, 0, Color.BLACK);

				currOrderPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder(edgeBorder, spacerBorder), editableBorder));
				ArrayList<Integer> temp = ((OrderListPanel) parent).unfoldedOrderIDs;
				if (((OrderListPanel) parent).unfoldedOrderIDs.contains(currOrder.getId())) {
					foldState = FoldablePanel.FOLD_STATE.OPEN;
				} else {
					foldState = FoldablePanel.FOLD_STATE.CLOSED;
				}
				currOrderPanel.setHeaderAndFoldUp(new OrderHeaderPanel(currOrder, currOrderPanel), new OrderDetailsPanel(currOrder, currOrderPanel), foldState);

				// if (Users.currentUser.isAdmin || (Users.currentUser.isOrderer && Users.currentUser == currOrder.getOrderedBy())) {
				if (Users.currentUser.canEditOrder(currOrder)) {
					currOrderPanel.addSecretCheck(i, new SecretListener());
				}

				currOrderPanel.setAlignmentY(TOP_ALIGNMENT);
				displayedOrderPanels.put(currOrder.getId(), currOrderPanel);

				OrderMainGUI.allFoldables.add(currOrderPanel);

				add(currOrderPanel);

				((OrderDetailsPanel) currOrderPanel.foldUpPanel).approvalPanel.setBackgroundColors();
			}
		}
		// revalidate();
		// repaint();
		DateTime t1 = new DateTime();
		Duration d0 = new Duration(t0, t1);
		System.out.println(d0.getMillis());
	}

	public static void setBackgroundIfReady(Component component, STATE state) {
		Color c = Colors.getColor(Colors.DEFAULT_EVEN);

		switch (state) {
			case WAITING_FOR_PHOTOEDIT:
				c = Colors.getColor(Colors.WAITING_FOR_PHOTOEDIT);
				break;
			case WAITING_FOR_APPROVAL:
				c = Colors.getColor(Colors.WAITING_FOR_APPROVAL);
				break;
			case APPROVED:
				c = Colors.getColor(Colors.APPROVED);
				break;
			case COMPLETED:
				c = Colors.getColor(Colors.COMPLETED);
				break;
			case CLOSED:
				c = Colors.getColor(Colors.CLOSED);
				break;
			case ARCHIVED:
				c = Colors.getColor(Colors.ARCHIVED);
				break;
		}

		component.setBackground(c);
	}

	public void refresh(ArrayList<Integer> orderNumbers) {
		GRFXOrdersMain.pBarDlg.setTitle("Refreshing orders");
		GRFXOrdersMain.pBarDlg.setMax(orderNumbers.size() * 2);
		GRFXOrdersMain.pBarDlg.setValue(0);

		DateTime t0 = new DateTime();
		model.refresh(orderNumbers);
		DateTime t1 = new DateTime();
		createPanel();
		DateTime t2 = new DateTime();

		Duration d0 = new Duration(t0, t1);
		Duration d1 = new Duration(t1, t2);

		System.out.println(d0.getMillis() + "      " + d1.getMillis());
	}

	public class SecretListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			JCheckBox currCheck = (JCheckBox) e.getSource();
			int index = Integer.parseInt(currCheck.getText());

			for (int i = 0; i < OrderTree2.this.getComponentCount(); i++) {
				SingleOrderPanel currOrderPanel = (SingleOrderPanel) OrderTree2.this.getComponent(i);
				if (i != index) {
					currOrderPanel.setSecretCheck(false);
				} else {
					if (currCheck.isSelected()) {
						selectedOrder = currOrderPanel.model;
					} else {
						selectedOrder = null;
					}
				}
			}
			((OrderListPanel) parent).parent.btnEditOrder.setEnabled(currCheck.isSelected());
		}

	}
}
